package cn.edu.ctbu.kcsj.webapi;

import cn.edu.ctbu.kcsj.core.PageUtils;
import cn.edu.ctbu.kcsj.domain.Course;
import cn.edu.ctbu.kcsj.service.CourseService;
import cn.edu.ctbu.kcsj.utils.RUtil;
import cn.edu.ctbu.kcsj.vo.R;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

@RestController
@Slf4j
@RequestMapping("/webapi/course")
public class CourseRestController {
    @Autowired
    private CourseService courseService;
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name,
                               @RequestParam(value = "farm",defaultValue = "") String farm){
        //按cid排正序
        Sort sort=Sort.by(Sort.Direction.ASC,"cid");
        Page<Course>coursePage;
        if (StringUtils.isEmpty(name)){
            Pageable pageable=PageRequest.of(page,size,sort);
            coursePage=courseService.findAll(pageable);
        }else if (farm.equals("姓名")&&!StringUtils.isEmpty(name)){
            Course course=new Course();
            course.setCname(name);
            Pageable pageable=PageRequest.of(page,size,sort);
            ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("cname",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Course>example=Example.of(course,matcher);
            coursePage=courseService.findAll(example,pageable);
        }else{
            Course course=new Course();
            course.setCid(Integer.parseInt(name));
            Pageable pageable=PageRequest.of(page,size,sort);
            ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("cid",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Course>example=Example.of(course,matcher);
            coursePage=courseService.findAll(example,pageable);
        }
        PageUtils pageUtils=new PageUtils(coursePage.getContent(),coursePage.getTotalElements());
        return pageUtils;
    }
    /**
     * 新增课程方法 /webapi/course/insert
     * @param course
     * @return
     */
    @PostMapping("/insert")
    public R insert(@Valid Course course, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String errormsg=bindingResult.getFieldError().getDefaultMessage();
            log.info("新增课程时出现错误！{}",errormsg);
            return RUtil.error(-10,errormsg);
        }
        courseService.insert(course);
        return RUtil.success();
    }
    /**
     * 更新课程方法 /webapi/course/update
     * @param course
     * @return
     */
    @PutMapping("/update")
    public Course update(Course course){
        course=courseService.update(course);
        return course;
    }
    /**
     * 删除课程方法 /webapi/course/delete
     * @param tno
     * @return
     */
    @DeleteMapping("/delete/{tno}")
    public void delete(@PathVariable Integer tno){
        courseService.delete(tno);
    }
    @GetMapping("/get/{cid}")
    public Course get(@PathVariable Integer cid){
        Course course=courseService.getByCid(cid);
        return course;
    }
}
